package com.qfedu.video.service.impl;


import com.qfedu.video.entry.Speaker;
import com.qfedu.video.mapper.SpeakerMapper;
import com.qfedu.video.redis.JedisClientPool;
import com.qfedu.video.service.SpeakerService;
import com.qfedu.video.utils.JsonUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class SpeakerServiceImpl implements SpeakerService {

    @Autowired
    SpeakerMapper speakerMapper;


    @Autowired
    JedisClientPool jedisClientPool;

    public List<Speaker> showAllSpeaker() {

        /**
         *  先从redis中查找，如果有直接返回
         *  如果没有：从数据库中查询，返回给controller层，并且在redis存一份
         *
         *    set("speakerList","json字符串")
         */

        Boolean exists = jedisClientPool.exists("speakerList");
        if(exists){

            System.out.println("从缓存中查找数据");
            String jsonStr = jedisClientPool.get("speakerList");
            //需要使用jackson进行数据转换
            List<Speaker> speakers = JsonUtils.jsonToList(jsonStr, Speaker.class);

            return speakers;

        }else{
            System.out.println("从数据库中查找数据");
            List<Speaker> list = speakerMapper.selectAllSpeaker();

            String json = JsonUtils.objectToJson(list);
            jedisClientPool.set("speakerList",json);

            return list;
        }



    }
}
